/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package blancanieves.front;

import blancanieves.back.Blancanieves;
import blancanieves.back.Enanito;
import javax.swing.SwingWorker;

/**
 *
 * @author pabloramix
 */
public class EscenarioFrame extends javax.swing.JFrame {

    /**
     * Creates new form NewJFrame
     */
    public EscenarioFrame() {
        initComponents();

        dormilon = new Enanito("Dormilón", "/blancanieves/imagenes/dormilon.png", 25, 25);
        panelDormilon = new PersonajePanel(dormilon);
        minaPanel.add(panelDormilon);

        feliz = new Enanito("Feliz", "/blancanieves/imagenes/feliz.png",45,55);
        panelFeliz = new PersonajePanel(feliz);
        minaPanel.add(panelFeliz);

        grunnon = new Enanito("Gruñón", "/blancanieves/imagenes/grunnon.png", 30, 70);
        panelGrunnon = new PersonajePanel(grunnon);
        minaPanel.add(panelGrunnon);

        mocoso = new Enanito("Mocoso", "/blancanieves/imagenes/mocoso.png", 45, 65);
        panelMocoso = new PersonajePanel(mocoso);
        minaPanel.add(panelMocoso);

        mudito = new Enanito("Mudito", "/blancanieves/imagenes/mudito.png", 90, 80);
        panelMudito = new PersonajePanel(mudito);
        minaPanel.add(panelMudito);

        sabio = new Enanito("Sabio", "/blancanieves/imagenes/sabio.png", 50, 50);
        panelSabio = new PersonajePanel(sabio);
        minaPanel.add(panelSabio);

        timido = new Enanito("Timido", "/blancanieves/imagenes/timido.png", 20, 90);
        panelTimido = new PersonajePanel(timido);
        minaPanel.add(panelTimido);

        blancanieves = new Blancanieves();
        panelBlancanieves = new PersonajePanel(blancanieves);
        paseoPanel.add(panelBlancanieves);

        worker = new SwingWorker() {

            @Override
            protected Object doInBackground() throws Exception {
                
                Blancanieves.Estado estadoAntBlancanieves = Blancanieves.Estado.PARADA;
                Enanito.Estado estadoAntDormilon = Enanito.Estado.PARADO;
                Enanito.Estado estadoAntFeliz = Enanito.Estado.PARADO;
                Enanito.Estado estadoAntMudito = Enanito.Estado.PARADO;
                Enanito.Estado estadoAntGrunnon = Enanito.Estado.PARADO;
                Enanito.Estado estadoAntTimido = Enanito.Estado.PARADO;
                Enanito.Estado estadoAntSabio = Enanito.Estado.PARADO;
                Enanito.Estado estadoAntMocoso = Enanito.Estado.PARADO;
                
                boolean hayQueRefrescar;    
                
                while (true) {
                    
                    hayQueRefrescar = false;
                    
                    panelDormilon.setProgreso(dormilon.getProgreso());
                    
                    if (estadoAntDormilon != dormilon.getEstado()) {
                        estadoAntDormilon = dormilon.getEstado();
                        
                        panelDormilon.getParent().remove(panelDormilon);
                        
                        switch (estadoAntDormilon) {
                            case ESPERANDO:
                                comedorPanel.add(panelDormilon);
                                break;
                            case COMIENDO:
                                mesaPanel.add(panelDormilon);
                                break;
                            default:
                                minaPanel.add(panelDormilon);
                        }
                        
                        panelDormilon.setEstado(estadoAntDormilon.toString());
                        
                        hayQueRefrescar = true;
                    }
                    
                    panelFeliz.setProgreso(feliz.getProgreso());
                    
                    if (estadoAntFeliz != feliz.getEstado()) {
                        estadoAntFeliz = feliz.getEstado();
                        
                        panelFeliz.getParent().remove(panelFeliz);
                        
                        switch (estadoAntFeliz) {
                            case ESPERANDO:
                                comedorPanel.add(panelFeliz);
                                break;
                            case COMIENDO:
                                mesaPanel.add(panelFeliz);
                                break;
                            default:
                                minaPanel.add(panelFeliz);
                        }
                        
                        panelFeliz.setEstado(estadoAntFeliz.toString());
                        
                        hayQueRefrescar = true;
                    }
                    
                    panelMudito.setProgreso(mudito.getProgreso());
                    
                    if (estadoAntMudito != mudito.getEstado()) {
                        estadoAntMudito = mudito.getEstado();
                        
                        panelMudito.getParent().remove(panelMudito);
                        
                        switch (estadoAntMudito) {
                            case ESPERANDO:
                                comedorPanel.add(panelMudito);
                                break;
                            case COMIENDO:
                                mesaPanel.add(panelMudito);
                                break;
                            default:
                                minaPanel.add(panelMudito);
                        }
                        
                        panelMudito.setEstado(estadoAntMudito.toString());
                        
                        hayQueRefrescar = true;
                    }
                    
                    panelGrunnon.setProgreso(grunnon.getProgreso());
                    
                    if (estadoAntGrunnon != grunnon.getEstado()) {
                        estadoAntGrunnon = grunnon.getEstado();
                        
                        panelGrunnon.getParent().remove(panelGrunnon);
                        
                        switch (estadoAntGrunnon) {
                            case ESPERANDO:
                                comedorPanel.add(panelGrunnon);
                                break;
                            case COMIENDO:
                                mesaPanel.add(panelGrunnon);
                                break;
                            default:
                                minaPanel.add(panelGrunnon);
                        }
                        
                        panelGrunnon.setEstado(estadoAntGrunnon.toString());
                        
                        hayQueRefrescar = true;
                    }
                    
                    panelTimido.setProgreso(timido.getProgreso());
                    
                    if (estadoAntTimido != timido.getEstado()) {
                        estadoAntTimido = timido.getEstado();
                        
                        panelTimido.getParent().remove(panelTimido);
                        
                        switch (estadoAntTimido) {
                            case ESPERANDO:
                                comedorPanel.add(panelTimido);
                                break;
                            case COMIENDO:
                                mesaPanel.add(panelTimido);
                                break;
                            default:
                                minaPanel.add(panelTimido);
                        }
                        
                        panelTimido.setEstado(estadoAntTimido.toString());
                        
                        hayQueRefrescar = true;
                    }
                    
                    panelSabio.setProgreso(sabio.getProgreso());
                    
                    if (estadoAntSabio != sabio.getEstado()) {
                        estadoAntSabio = sabio.getEstado();
                        
                        panelSabio.getParent().remove(panelSabio);
                        
                        switch (estadoAntSabio) {
                            case ESPERANDO:
                                comedorPanel.add(panelSabio);
                                break;
                            case COMIENDO:
                                mesaPanel.add(panelSabio);
                                break;
                            default:
                                minaPanel.add(panelSabio);
                        }
                        
                        panelSabio.setEstado(estadoAntSabio.toString());
                        
                        hayQueRefrescar = true;
                    }
                    
                    panelMocoso.setProgreso(mocoso.getProgreso());
                    
                    if (estadoAntMocoso != mocoso.getEstado()) {
                        estadoAntMocoso = mocoso.getEstado();
                        
                        panelMocoso.getParent().remove(panelMocoso);
                        
                        switch (estadoAntMocoso) {
                            case ESPERANDO:
                                comedorPanel.add(panelMocoso);
                                break;
                            case COMIENDO:
                                mesaPanel.add(panelMocoso);
                                break;
                            default:
                                minaPanel.add(panelMocoso);
                        }
                        
                        panelMocoso.setEstado(estadoAntMocoso.toString());
                        
                        hayQueRefrescar = true;
                    }
                    
                    panelBlancanieves.setProgreso(blancanieves.getProgreso());
                    
                    if (estadoAntBlancanieves != blancanieves.getEstado()) {
                        estadoAntBlancanieves = blancanieves.getEstado();
                        
                        panelBlancanieves.getParent().remove(panelBlancanieves);
                        
                        switch (estadoAntBlancanieves) {
                            case COCINANDO:
                                cocinaPanel.add(panelBlancanieves);
                                break;
                            case SIRVIENDO:
                                mesaPanel.add(panelBlancanieves);
                                break;
                            default:
                                paseoPanel.add(panelBlancanieves);
                        }
                        
                        panelBlancanieves.setEstado(estadoAntBlancanieves.toString());
                        
                        hayQueRefrescar = true;
                    }
                    
                    if (hayQueRefrescar) {
                        repaint();
                    }
                    
                    Thread.sleep(10);
                }
            }
        };
        
        worker.execute();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        minaPanel = new javax.swing.JPanel();
        comedorPanel = new javax.swing.JPanel();
        cocinaPanel = new javax.swing.JPanel();
        paseoPanel = new javax.swing.JPanel();
        mesaPanel = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Actividad 3 - Blancanieves");
        setResizable(false);

        minaPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Mina"));
        minaPanel.setToolTipText("");
        minaPanel.setName("Prueba"); // NOI18N
        minaPanel.setLayout(new javax.swing.BoxLayout(minaPanel, javax.swing.BoxLayout.Y_AXIS));

        comedorPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Comedor"));
        comedorPanel.setToolTipText("");
        comedorPanel.setName("Prueba"); // NOI18N
        comedorPanel.setLayout(new javax.swing.BoxLayout(comedorPanel, javax.swing.BoxLayout.Y_AXIS));

        cocinaPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Cocina"));
        cocinaPanel.setToolTipText("");
        cocinaPanel.setName("Prueba"); // NOI18N
        cocinaPanel.setLayout(new javax.swing.BoxLayout(cocinaPanel, javax.swing.BoxLayout.Y_AXIS));

        paseoPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("De paseo"));
        paseoPanel.setToolTipText("");
        paseoPanel.setName("Prueba"); // NOI18N
        paseoPanel.setLayout(new javax.swing.BoxLayout(paseoPanel, javax.swing.BoxLayout.Y_AXIS));

        mesaPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Mesa"));
        mesaPanel.setToolTipText("");
        mesaPanel.setName("Prueba"); // NOI18N
        mesaPanel.setLayout(new javax.swing.BoxLayout(mesaPanel, javax.swing.BoxLayout.Y_AXIS));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(minaPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 333, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(comedorPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 333, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(mesaPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 333, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(cocinaPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 333, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(paseoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 333, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(comedorPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(mesaPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 500, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(cocinaPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(paseoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(minaPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 634, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        mesaPanel.getAccessibleContext().setAccessibleName("Mesa");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(EscenarioFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(EscenarioFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(EscenarioFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(EscenarioFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new EscenarioFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel cocinaPanel;
    private javax.swing.JPanel comedorPanel;
    private javax.swing.JPanel mesaPanel;
    private javax.swing.JPanel minaPanel;
    private javax.swing.JPanel paseoPanel;
    // End of variables declaration//GEN-END:variables

    private Enanito dormilon;
    private Enanito feliz;
    private Enanito grunnon;
    private Enanito mocoso;
    private Enanito sabio;
    private Enanito timido;
    private Enanito mudito;

    private Blancanieves blancanieves;

    private PersonajePanel panelDormilon;
    private PersonajePanel panelFeliz;
    private PersonajePanel panelGrunnon;
    private PersonajePanel panelMocoso;
    private PersonajePanel panelSabio;
    private PersonajePanel panelTimido;
    private PersonajePanel panelMudito;

    private PersonajePanel panelBlancanieves;

    private final SwingWorker worker;
}
